package com.innjia.base.utils;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 生成合同工具
 * @author fangcheng.huang
 *
 */
public class ContractTemplateUtil {

	private static Logger logger = Logger.getLogger(ContractTemplateUtil.class);
	
	/**
	 * 根据模板填写合同信息
	 * @param map
	 * @param contractTempalte
	 * @return
	 * String
	 */
	public static String contractGenerationResult(Map<String, Object> map, String contractTempalte) {
		Configuration cfg = new Configuration();
		StringTemplateLoader loader = new StringTemplateLoader();
		loader.putTemplate("contractT", contractTempalte);
		cfg.setTemplateLoader(loader);
		try {
			Template template = cfg.getTemplate("contractT");
			StringWriter writer = new StringWriter();
			template.process(map, writer);
			return writer.toString();
		} catch (Exception e) {
			logger.info("======根据模板填写合同信息异常!", e);
		}
		return null;
	}
	
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "张三");
		System.out.println(contractGenerationResult(map, "name:${name}"));
	}
}
